﻿using System;
using System.Linq;
using FluentValidation;

namespace LINQPad.OpenAccess.UI.ViewModel
{
    /// <summary>
    /// Base class for driver sub settings view models.
    /// </summary>
    /// <typeparam name="TViewModel">The type of the view model.</typeparam>
    public class DriverSubSettingsViewModelBase<TViewModel> : ValidatableViewModelBase<TViewModel>
        where TViewModel : ValidatableViewModelBase
    {
        #region Private

        #region - Vars

        private DriverSettingsViewModel _owner;

        #endregion

        #endregion

        #region Protected

        #region - Functions

        /// <summary>
        /// Accepts the owner.
        /// </summary>
        /// <param name="owner">The owner.</param>
        protected virtual void AcceptOwner( DriverSettingsViewModel owner )
        {

        }
        /// <summary>
        /// Called when the assigned owner has changed.
        /// </summary>
        /// <param name="newOwner">The new owner.</param>
        /// <param name="oldOwner">The old owner.</param>
        protected virtual void OnOwnerChanged( DriverSettingsViewModel newOwner, DriverSettingsViewModel oldOwner )
        {
            if ( oldOwner != null )
                this.ReleaseOwner( oldOwner );
            if ( newOwner != null )
                this.AcceptOwner( newOwner );
        }
        /// <summary>
        /// Releases the owner.
        /// </summary>
        /// <param name="owner">The owner.</param>
        protected virtual void ReleaseOwner( DriverSettingsViewModel owner )
        {

        }

        #endregion

        #endregion

        #region Public

        #region - Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DriverSubSettingsViewModelBase{TViewModel}" /> class.
        /// </summary>
        /// <param name="validator">The validator.</param>
        public DriverSubSettingsViewModelBase( IValidator<TViewModel> validator = null )
            : base( validator ) { }

        #endregion

        #region - Properties

        /// <summary>
        /// Gets or sets the owner.
        /// </summary>
        /// <value>
        /// The owner.
        /// </value>
        public DriverSettingsViewModel Owner
        {
            get
            {
                return this._owner;
            }
            set
            {
                if ( this._owner != value )
                {
                    var oldOwner = this._owner;

                    this._owner = value;

                    this.OnOwnerChanged( value, oldOwner );
                    this.OnPropertyChanged( ( ) => this.Owner );
                }
            }
        }

        #endregion

        #endregion

        #region ValidatableViewModelBase<TViewModel> Members

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose( bool disposing )
        {
            if ( disposing )
            {
                if ( this._owner != null )
                {
                    this.ReleaseOwner( this._owner );
                    this._owner = null;
                }
            }
            base.Dispose( disposing );
        }
        /// <summary>
        /// Invalidates this instance.
        /// </summary>
        public override void Invalidate( )
        {
            base.Invalidate( );
            var owner = this.Owner;
            if ( owner != null )
                owner.Invalidate( );
        }

        #endregion
    }
}
